package com.joyintech.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.activiti.engine.*;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.activiti.spring.boot.ActivitiProperties;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @Author yiyoung
 * @date 2020/4/21
 */
@Configuration
public class ActivitiConfig  extends AbstractProcessEngineAutoConfiguration  implements ProcessEngineConfigurationConfigurer{

    @Bean("activitiDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.activiti.datasource")
    public DataSource activitiDataSource() {

        return DataSourceBuilder.create().build();
    }

    /**
     * 流程配置，与spring整合采用SpringProcessEngineConfiguration这个实现
     * @param processEngineConfiguration
     * @param activitiProperties
     * @return
     */
    @Bean
    public ProcessEngineConfiguration processEngineConfiguration(ProcessEngineConfiguration processEngineConfiguration,
                                                                 ActivitiProperties activitiProperties){
        processEngineConfiguration.setDataSource(activitiDataSource());
        return processEngineConfiguration;
    }

    /**
     * 流程引擎，与spring整合使用factoryBean
     * @param processEngineConfiguration
     * @return
     */
    @Bean
    public ProcessEngineFactoryBean processEngine(ProcessEngineConfiguration processEngineConfiguration){
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);
        return processEngineFactoryBean;
    }

    /**
     * RepositoryService
     * @param processEngine
     * @return
     */
    @Bean
    public RepositoryService repositoryService(ProcessEngine processEngine){ return processEngine.getRepositoryService(); }

    @Bean
    public RuntimeService runtimeService(ProcessEngine processEngine){ return processEngine.getRuntimeService(); }

    @Bean
    public TaskService taskService(ProcessEngine processEngine){ return processEngine.getTaskService(); }

    @Bean
    public HistoryService historyService(ProcessEngine processEngine){ return processEngine.getHistoryService();}


    @Bean
    public ManagementService managementService(ProcessEngine processEngine){ return processEngine.getManagementService(); }

    @Bean
    public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine){ return processEngine.getDynamicBpmnService();}

    //八大接口 end

    /**
     * 一些工具bean
     */

    /**
     * jackson xml util
     * @return
     */
    @Bean
    public ObjectMapper objectMapper(){
        return new ObjectMapper();
    }

    @Override
    public void configure(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
      //  springProcessEngineConfiguration.setActivityFontName("宋体");
      //  springProcessEngineConfiguration.setAnnotationFontName("宋体");
     //   springProcessEngineConfiguration.setLabelFontName("宋体");
    }
}
